var jq = jQuery.noConflict(); // jQuery $ 符号改写
var vConsole = new VConsole(); //手机调试js  生产环境可删除
var app = angular.module('myserver', [])

app.run(['$rootScope','wxAlert', function ($rootScope,wxAlert) {



    //执行获取地理位置的函数
    if (navigator.geolocation) {
        $rootScope.xxxx = 1
        navigator.geolocation.getCurrentPosition(showPosition);
     
    } else {
        $rootScope.xxxx = 2
        alert("抱歉!浏览器限制,不支持定位");
    }

    //获取成功时回调函数
    function showPosition(position) {
        console.log(position, 'position');


    }


}])
app.factory('wxAlert', [function () {
    var el, type, html = '',
        $$ = angular.element;

    function createAlert() {
        //有dom则不生成dom
        el = $$("#wxalert");
        if (el.size() == 0) {
            el = $$('<div id="wxalert"></div>');
            $$("body").append(el);
        }
    }
    function createHtml(opt, btns) {
        var h = opt.mask ? '<div class="alert-mask"></div>' : '';
        h += '<div class="alert-content">' +
            '<div>'+ opt.info +'</div>'
            '</div>';
        return h;
    }

    function tooltip(msg,time){
        var _timer;
        var config = {
            mask: true, //遮罩层
            info: msg, //提示文本
            timeout: time || 3 //关闭时间，默认1s关闭提示框
        };
        var h = createHtml(config, "");
        createAlert();
        el.html(h).show().find(".alert-mask").one("click", function () {
            clearInterval(_timer);
            el.hide();
        });
        el.find(".alert-content").one("click",function(){
            clearInterval(_timer);
            el.hide();
        })
        _timer = setTimeout(function () {
            el.find(".alert-mask").trigger("click");
        }, config.timeout * 1000);
        
     
    }
    return {
        tooltip:tooltip
    }
}])
app.service('fetch', ['$rootScope', '$q', function ($rootScope, $q) {
    _ajax = function (_type, _url, _data) {
        var dtd = $q.defer();
        jq.ajax({
            type: _type,
            url: _url,
            data: _data ? _data : ''
        }).done(function (res) {
            if (res.code === 0 || res.code === -1) {

                dtd.resolve(res)

            } else {

                dtd.reject(res)

            }
        }).fail(function (err) {
            dtd.reject(err)
        })
        return dtd.promise
    }

    /*get*/
    this.ajaxget = function (url, data) {
        return _ajax('get', url, data)
    }

    /*post*/
    this.ajaxpost = function (url, data) {
        return _ajax('post', url, data)
    }
}])

app.factory('axios', ['fetch', '$rootScope','wxAlert', function (fetch, $rootScope,wxAlert) {

    //根据定位查询门店
    function shops(data) {
        var v = {
            userLongtitude: 113.406246,
            userLatitude: 23.110533
        }
        return fetch.ajaxpost("https://haipengwandou.mynatapp.cc/shop/recommend", v)
    }

    //搜索门店
    function search(data) {
        var searchData = {
            shopAddressKeyword: data
        }
        return fetch.ajaxget('https://haipengwandou.mynatapp.cc/shop/search', searchData)
    }

    //门店全部经理信息
    function getByShop(getByShopid) {

        var shopData = {
            shopId: getByShopid
        }
        return fetch.ajaxget('https://haipengwandou.mynatapp.cc/saleman/getByShopId', shopData)
    }

    /*门店个人经理信息*/
    function BySaleId(BySaleIdid) {

        var byId = {
            salemanId: BySaleIdid
        }
        return fetch.ajaxget('https://haipengwandou.mynatapp.cc/saleman/getBySaleId', byId)
    }
    /*提交评价*/
    function scoreItem(_data) {
        return fetch.ajaxpost('https://haipengwandou.mynatapp.cc/saleman/postScoreItem', _data)

    }
    return {
        shops: shops,
        getByShop: getByShop,
        search: search,
        BySaleId: BySaleId,
        scoreItem: scoreItem
    }
}])
